﻿// NewGameApp.cpp : 定义应用程序的入口点。
//

#include "framework.h"
#include "NewGameApp.h"
#include "WL.h"
#include "DefMemory.h"
#include <iostream>
extern "C"
{
	int (*wlMain)(HINSTANCE hInstance, HINSTANCE h, int nShow) = nullptr;
}



typedef struct { char m; } yes;
typedef struct { char m[2]; } no;

template <typename B, typename D>
struct Host
{
	operator B* ()const;
	operator D* ();
};


static int ssa = 0;
template <typename B, typename D>
struct is_base_of
{
	template <typename T>
	static yes check(D*, T);
	static no check(B*, int);
	static const bool value = sizeof( check( Host<B, D>(), int() ) ) == sizeof(yes);
};

//Test sample
class Base {};
class Derived : private Base {};
enum
{
	kDefaultMemoryAlignment = 16
};


template<typename T, int memlabel = 1, int align = kDefaultMemoryAlignment >
class stl_allocator
{
public:
	typedef size_t    size_type;
	typedef std::ptrdiff_t difference_type;
	typedef T* pointer;
	typedef const T* const_pointer;
	typedef T& reference;
	typedef const T& const_reference;
	typedef T         value_type;


	template <typename U>
	struct rebind
	{
		typedef stl_allocator<U, memlabel, align> other;
	};


	stl_allocator()
	{
	}
	stl_allocator(const stl_allocator& alloc) throw()
	{
		
	}
	template <typename U, int _memlabel, int _align> stl_allocator(const stl_allocator<U, _memlabel, _align>& alloc) throw()
	{
		
	}
	~stl_allocator() throw()
	{
	}

	pointer address(reference x) const
	{
		return &x;
	}

	const_pointer address(const_reference x) const
	{
		return &x;
	}

	pointer allocate(size_type count, void const* /*hint*/ = 0)
	{
		return 0;
	}

	void deallocate(pointer p, size_type /*n*/)
	{
	}

	template <typename U, int _memlabel, int _align>
	bool operator== (stl_allocator<U, _memlabel, _align> const& a) const
	{
		return _memlabel == memlabel && get_root_ref() == a.get_root_ref();

	}
	template <typename U, int _memlabel, int _align>
	bool operator!= (stl_allocator<U, _memlabel, _align> const& a) const
	{
		return _memlabel != memlabel || get_root_ref() != a.get_root_ref();
	}

	size_type max_size() const throw()
	{
		return 0x7fffffff;
	}

	void construct(pointer p, const T& val)
	{
		new (p) T(val);
	}

	void destroy(pointer p)
	{
		p->~T();
	}
};


struct RTTI
{
	RTTI* base;       // super rtti class
	const char* className;  // the name of the class
	int                      size;       // sizeof (Class)
	bool                     isAbstract; // is the class Abstract?
	bool                     isSealed;
	bool                     isDeprecated;
};

#define STL_ALLOCATOR(label, type) ::stl_allocator<type, label>

typedef std::map<int, RTTI, std::less<int>, STL_ALLOCATOR(0, int) > RTTIMap;


int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                      _In_opt_ HINSTANCE hPrevInstance,
                      _In_ LPWSTR    lpCmdLine,
					  _In_ int       nCmdShow)
{
	// TODO: 在此处放置代码。
	int ret = 0;
	UNUSED(lpCmdLine);
	try 
	{
		//会抛出异常的代码
		HMODULE hGame = nullptr;
		hGame = LoadLibraryA("WLEngine.dll");
		if (nullptr != hGame)
		{
			do
			{
				wlMain = (int (*)(HINSTANCE, HINSTANCE, int))GetProcAddress(hGame, "wlMain");
				if (nullptr == wlMain)
				{
					MessageBoxA(NULL, "load dll failed", "Error", MB_OK | MB_ICONWARNING);
					continue;
				}
				ret = wlMain(hInstance, hPrevInstance, nCmdShow);
				FreeLibrary(hGame);
			} while (false);
		}
	}
	catch (...)   //捕获任何异常
	{
		ret = -1;
	}
	return ret;
}


